package com.ningmengs789.live.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ningmengs789.live.Interface.RequestCallback;
import com.ningmengs789.live.R;
import com.ningmengs789.live.http.OKHttpUtils;
import com.ningmengs789.live.live.live.common.widget.gift.utils.OnItemClickListener;
import com.ningmengs789.live.net.response.CountryBean;
import com.ningmengs789.live.net.utils.NToast;
import com.ningmengs789.live.ui.adapter.CountryAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CountriesActivity extends BaseActivity implements OnItemClickListener<CountryBean> {

    @BindView(R.id.tv_title_center)
    TextView tv_title_center;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private CountryAdapter mAdapter;

    private static String[] INITIAL_TABLE = {"A", "B", "C", "D", "E", "F", "G", "H", "J", "K",
            "L", "M", "N", "O", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"};
    private ArrayList<CountryBean> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_countries);

        ButterKnife.bind(this);
        setHeadLayout();
        initEvent();

        mobileCode = getIntent().getStringExtra("MOBILE_CODE");

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mAdapter = new CountryAdapter(mContext);
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);

        getCountryList();
    }

    private void initEvent() {
        tv_title_center.setVisibility(View.VISIBLE);
        tv_title_center.setText(getString(R.string.sc_17));
    }

    private void getCountryList(){
        OKHttpUtils.getInstance().getRequest("app/sms/getCountryList", "", new RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                NToast.shortToast(mContext, errInfo);
            }
            @Override
            public void onSuccess(String result) {
                if (mList != null && mList.size() > 0){
                    mList.clear();
                }
                try {
                    JSONObject data = new JSONObject(result);
                    JSONObject listJO = data.getJSONObject("list");
                    for (int i = 0; i < INITIAL_TABLE.length; i++) {
                        if(listJO.has(INITIAL_TABLE[i])){
                            JSONArray ja = listJO.getJSONArray(INITIAL_TABLE[i]);
                            if (ja != null && ja.length() > 0){
                                for (int j = 0; j < ja.length(); j++) {
                                    JSONObject content = ja.getJSONObject(j);
                                    if (j == 0){
                                        mList.add(new CountryBean(INITIAL_TABLE[i], content.getString("country_name"), content.getString("mobile_code")));
                                    }else {
                                        mList.add(new CountryBean(null, content.getString("country_name"), content.getString("mobile_code")));
                                    }
                                }
                            }
                        }
                    }
                    mAdapter.addData(mList, mobileCode);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private String mobileCode = "";

    @Override
    public void onItemClick(CountryBean bean, int position) {
        setResult(1002, new Intent().putExtra("MOBILE_CODE", bean.getMobile_code()));
        finish();
    }
}
